package com.quanyan.place.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.*;
import com.quanyan.place.entity.apireq.ReqSetPlacePeriod;
import com.quanyan.place.entity.apiresp.RespBizPeriodInfo;
import com.quanyan.place.mapper.TbBizPlacePeriodMapper;
import com.quanyan.place.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * Created by Administrator on 2016/4/14 0014.
 */
@Service("periodService")
public class PeriodServiceImpl implements PeriodService {

    private Logger logger = LoggerFactory.getLogger(PeriodServiceImpl.class);

    @Autowired
    TbBizPlacePeriodMapper tbBizPlacePeriodMapper;
    @Autowired
    private RepresentService representService;

    @Autowired
    PlaceBaseService placeBaseService;

    @Autowired
    TbBusinessLogService tbBusinessLogService;

    @Autowired
    TbBizPlacePeriodService tbBizPlacePeriodService;

    @Override
    public void importPeriodData(Integer placeId) {
        //插入场馆锁定周期，解锁周期
        TbBizPlacePeriod vo=new TbBizPlacePeriod();
        vo.setJoinPrivateTime(new Date());
        vo.setPlaceId(placeId);
        Date d= DateUtils.addNDay(new Date(),PlaceConstants.LOCK_DAYS);
        vo.setValidTime(d);
        vo.setFirstLockDays(PlaceConstants.LOCK_DAYS);
        vo.setEndLockDays(PlaceConstants.LOCK_DAYS);
        vo.setUnlockDays(PlaceConstants.UNLOCK_DAYS);
        vo.setOriginalValidTime(d);
        tbBizPlacePeriodMapper.insert(vo);
    }

    /**
     * 通过场馆id获取信息
     * @param placeId
     * @return
     */
    @Override
    public TbBizPlacePeriod getPeriodInfoByPlaceId(Integer placeId) {
        TbBizPlacePeriodExample example =new TbBizPlacePeriodExample();
        TbBizPlacePeriodExample.Criteria eCriteria=example.createCriteria();
        eCriteria.andPlaceIdEqualTo(placeId);
        List<TbBizPlacePeriod> list=tbBizPlacePeriodMapper.selectByExample(example);
        if (list!=null && list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }

    @Override
    public RespBizPeriodInfo getDataByPlaceId(Integer placeId) {
        TbBizPlacePeriod vo= this.getPeriodInfoByPlaceId(placeId);
        RespBizPeriodInfo periodInfo=new RespBizPeriodInfo();
        if (vo!=null) {
            if (vo.getValidTime()!=null) {
                periodInfo.setTime(DateUtils.transferDateToString(vo.getValidTime()));
            }
        }
        TbBizPlaceRepresent represent=representService.getRepresentInfoByPlaceId(placeId,1);
        if (represent!=null){
            periodInfo.setBusinessRepresent(represent.getBusinessRepresent());
        }
       TbBizPlace place= placeBaseService.getPlaceById(placeId);
        if (place!=null){
            periodInfo.setPlaceName(place.getName());
            periodInfo.setPlaceId(place.getId());
        }
     return  periodInfo;
    }


    @Override
    public APIResponse updatePlaceLockDays(ReqSetPlacePeriod reqSetPlacePeriod) {

        List<TbBizPlacePeriod> listOld = tbBizPlacePeriodService.getPeriodInfoByPlaceId(reqSetPlacePeriod.getPlaceId());
        //Date currentTime = new Date();
        if (listOld.size() > 0) {
            TbBizPlacePeriod vo = listOld.get(0);
            if (DateUtils.dateDiffDay(vo.getOriginalValidTime(),reqSetPlacePeriod.getNewValidTime())>PlaceConstants.PLACE_DELAY_VALID_DAYS){
                return APIResponse.returnFail("延长时间不能超过30天");
            }
            TbBizPlacePeriod tp = new TbBizPlacePeriod();
                String oldTime = DateUtils.transferDateToString(vo.getValidTime());
                String newTime=DateUtils.transferDateToString(reqSetPlacePeriod.getNewValidTime());
                if (!newTime.equals(oldTime)) {
                    int days = DateUtils.dateDiffDay(reqSetPlacePeriod.getValidTime(), reqSetPlacePeriod.getNewValidTime());
                    Date d = DateUtils.addNDay(vo.getValidTime(), days);//锁定截止时间
                    tp.setEndLockDays(vo.getEndLockDays() + days);//最终锁定天数
                    //  tp.setUnlockDays(vo.getUnlockDays()+reqSetPlacePeriod.getUnlockDays());
                    tp.setValidTime(d);
                    TbBizPlacePeriodExample periodExample = new TbBizPlacePeriodExample();
                    TbBizPlacePeriodExample.Criteria periodCriteria = periodExample.createCriteria();
                    periodCriteria.andPlaceIdEqualTo(reqSetPlacePeriod.getPlaceId());
                    tbBizPlacePeriodMapper.updateByExampleSelective(tp, periodExample);
                    //记录日志
                    TbBusinessLog log = new TbBusinessLog();
                    log.setEntityId(vo.getId());
                    log.setEntityType(PlaceConstants.LOG_TYPE_PLACE);
                    log.setUid(reqSetPlacePeriod.getUid());
                    log.setLogName("设置场馆解锁及锁定天数");
                    log.setLogInfo("场馆锁定时效old：" + oldTime + ";" + "场馆锁定延长天数new：" +newTime);
                    tbBusinessLogService.insertLogInfo(log);
                }
        }
        return APIResponse.returnSuccess();
    }


}
